$().ready(function() {
	loadType();
	loadType1();
	validateRule();
});


$(function () {
    $("input[type='radio']").on("click",function () {
        if($("input[id='0']:checked").val()=="0"){
        	//$("#contentDiv").hide();
        	//$("contentDiv").css('display',"none");
        	//$("#goodsDiv").show();
        	//$("goodsDiv").css('display',"inline");
        	document.getElementById("contentDiv").style.visibility="hidden";//隐藏

        	document.getElementById("goodsDiv").style.visibility="visible";//显示
        	// $(".chosen-select-content").empty();
        	
        }else if($("input[id='1']:checked").val()=="1"){
            console.log("2222");
            document.getElementById("goodsDiv").style.visibility="hidden";//隐藏

        	document.getElementById("contentDiv").style.visibility="visible";//显示
           
        }else{
        	document.getElementById("goodsDiv").style.visibility="hidden";//隐藏
        	document.getElementById("contentDiv").style.visibility="hidden";//隐藏
        	
        	
        	console.log("33333");
        }
    })
})

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	var a = $("#logo").find("div").length;
	console.log("图片数量："+a);
	if (a > 9) {
		layer.alert("图片最多为9张");
	} else {
		$("#images").val(getImg());
		$.ajax({
			cache : true,
			type : "POST",
			url : "/wx/cmsMaterial/save",
			data : $('#signupForm').serialize(),// 你的formid
			async : false,
			error : function(request) {
				parent.layer.alert("Connection error");
			},
			success : function(data) {
				if (data.code == 0) {
					parent.layer.msg("操作成功");
					parent.reLoad();
					var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
					parent.layer.close(index);

				} else {
					parent.layer.alert(data.msg)
				}

			}
		});
	}

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			}
		}
	})
}

function loadType(){
	console.log("加载商品--------------------")
	var html = "";
	$.ajax({
		url : '/wx/goodsGoods/getGoods',
		success : function(data) {
			// console.log(data);
			data = data.data;
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
				// console.log(data[i])
			}
			// console.log("循环结束");
			$(".chosen-select").append(html);
			$(".chosen-select").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}

function loadType1(){
	console.log("加载文章--------------------")
	var html = "";
	$.ajax({
		url : '/wx/lessonContent/getLessonContent',
		success : function(data) {
			 console.log(data);
			data = data.data;
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
				// console.log(data[i])
			}
			// console.log("循环结束");
			$(".chosen-select-content").append(html);
			$(".chosen-select-content").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-content').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}


var imgId=0;
//多图片上传
layui.use('upload', function () {
	var upload = layui.upload;
	//执行实例
	var uploadInst = upload.render({
		elem: '#file_upload' //绑定元素
		,url: '/common/sysFile/upload' //上传接口
		,multiple: true
		,accept: 'file'
		,done: function(r){
			//上传完毕
			layer.msg(r.msg);
			imgId++;
			// app.getData();
			$("#logo").append("<div id='imgId"+imgId+"' class='divImg' >" +
				"<img  src='" + r.fileName+ "' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +
				"<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('imgId"+imgId+"')\"></i></div>");

			// $("#images").val($("#images").val()+","+r.fileName);
		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});

function getImg(){
	// var imgs='';
	var img='';
	$("#logo").find("div").each(function(){
		$(this).find("img").each(function(){
			img=img + ','+$(this).attr("src");
		});
		// imgs;
	});
	return img;
}

function delImg(id){
	$("#"+id).remove();
}

// upload.render({
// 	elem: '#file_upload', //绑定元素
// 	url: '/common/sysFile/upload', //上传接口
// 	size: 10000,
// 	accept: 'file',
// 	done: function (r) {
// 		layer.msg(r.msg);
// 		// app.getData();
// 		$("#logo").html("<img src='" + r.fileName + "' style='width:80px;height:80px;'>");
// 		$("#img").val(r.fileName);
// 	},
// 	error: function (r) {
// 		layer.msg(r.msg);
// 	}
// });